package com.xbzl.cly_demo.mqtt;

import com.xbzl.cly_demo.entity.VehicleStatus;
import com.xbzl.cly_demo.properties.MqttProperties;
import com.xbzl.cly_demo.service.VehicleService;
import com.xbzl.cly_demo.service.VehicleStatusService;
import com.xbzl.cly_demo.utils.MessageConverter;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MqttMsgCallback implements MqttCallbackExtended {
    @Autowired
    MqttProperties mqttProperties;
    @Autowired
    VehicleStatusService vehicleStatusService;

    MqttClient mqttClient;
    @Autowired
    VehicleService vehicleService;
    @PostConstruct
    public void initMqttClient() throws MqttException {
        MqttConnectOptions options = new MqttConnectOptions();
        // 配置 MQTT 客户端
        mqttClient = new MqttClient(mqttProperties.getUrl(), mqttProperties.getClientId());
        mqttClient.setCallback(this);
        mqttClient.connect(options);  // 连接到 MQTT Broker
        System.out.println("Connected to MQTT Broker: " + mqttProperties.getUrl());
        // 订阅消息
        mqttClient.subscribe(mqttProperties.getSubTopics());
        System.out.println("Subscribed to topic: " + mqttProperties.getSubTopics());
    }

    public void connectComplete(boolean reconnect, String serverUri) {
        // 连接建立成功逻辑
        // 此处一般需要实现主题订阅
        System.out.println("success");
    }

    @Override
    public void connectionLost(Throwable throwable) {

    }

    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        // 接收到消息，根据需求完成消息处理逻辑： 解析，入库等
        String message = new String(mqttMessage.getPayload());
//        System.out.println("topic:" + topic + ", payload:" + message);
        processMessage(topic,message);
    }

    private void processMessage(String topic, String message) {
        try{

//            vehicleService.save(MessageConverter.convertMessageToVehicle(message));
            VehicleStatus vehicleStatus = MessageConverter.convertMessageToVehicleStatus(message);
            System.out.println(vehicleStatus);
//            vehicleStatusService.save(vehicleStatus);
//            log.info("成功插入数据{}",vehicleStatus);
            System.out.println("topic:" + topic + ", payload:" + message);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }
}




//package com.xbzl.cly_demo.mqtt;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.xbzl.cly_demo.entity.VehicleLocation;
//import com.xbzl.cly_demo.properties.MqttProperties;
//import com.xbzl.cly_demo.utils.MessageConverter;
//import jakarta.annotation.PostConstruct;
//import org.eclipse.paho.client.mqttv3.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//@Component
//public class MqttMsgCallback implements MqttCallbackExtended {
//    @Autowired
//    MqttProperties mqttProperties;
//
//    MqttClient mqttClient;
//    @PostConstruct
//    public void initMqttClient() throws MqttException {
//        MqttConnectOptions options = new MqttConnectOptions();
//        // 配置 MQTT 客户端
//        mqttClient = new MqttClient(mqttProperties.getUrl(), mqttProperties.getClientId());
//        mqttClient.setCallback(this);
//        mqttClient.connect(options);  // 连接到 MQTT Broker
//        System.out.println("Connected to MQTT Broker: " + mqttProperties.getUrl());
//        // 订阅消息
//        mqttClient.subscribe(mqttProperties.getSubTopics());
//        System.out.println("Subscribed to topic: " + mqttProperties.getSubTopics());
//
//    }
//
//    public void connectComplete(boolean reconnect, String serverUri) {
//        // 连接建立成功逻辑
//        // 此处一般需要实现主题订阅
//        System.out.println("success");
//    }
//
//    @Override
//    public void connectionLost(Throwable throwable) {
//
//    }
//
//    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
//        // 接收到消息，根据需求完成消息处理逻辑： 解析，入库等
//        String message = new String(mqttMessage.getPayload());
//        System.out.println("topic:" + topic + "\n\n, payload:" + message + "\n\n");
//        processMessage(topic,message);
//    }
//
//
//    private final ObjectMapper objectMapper = new ObjectMapper();
//
//    private void processMessage(String topic, String message) {
//        try{
//            VehicleLocation vl = MessageConverter.convertMessageToVehicleLocation(message);
//            System.out.println(vl);
//            System.out.println(MessageConverter.convertMessageToVehicleAbnormalRecords(message));
//            System.out.println(MessageConverter.convertMessageToVehicles(message));
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//
//    @Override
//    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
//
//    }
//}
//
